/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - system/io
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AESearchPath.cpp
// Author:		Gianluca Belardelli
// Date:		10/10/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

AESearchPath::AESearchPath( const char *lpBasePath, AEBOOL32 bWritable ) : m_bWritable( bWritable )
{
	AEASSERT_MSG( lpBasePath[0] == '$', "AESearchPath::AESearchPath Il path deve iniziare con $" );

	AEINT32 nLen = strlen( lpBasePath );

	m_lpBasePath = new char[ nLen +  1 ];
	strcpy( m_lpBasePath, lpBasePath );

	if( m_lpBasePath[ nLen - 1 ] == '/' )
		m_lpBasePath[ nLen - 1 ] = '\0';

	const char *lpRoot = strstr( lpBasePath, "/" );

	if( lpRoot )
	{
		nLen = (lpRoot-lpBasePath) + 2;
		m_lpRootName = new char[ nLen ];
		memset( m_lpRootName, 0, nLen );
		strncpy( m_lpRootName, &lpBasePath[1], nLen-3 );
	}
	else
	{
		nLen = strlen( lpBasePath );
		m_lpRootName = new char[ nLen ];
		memset( m_lpRootName, 0, nLen );
		strncpy( m_lpRootName, &lpBasePath[1], nLen );
	}

	m_lpPathBelowRoot = (m_lpBasePath+nLen-1);
}

AESearchPath::~AESearchPath( void )
{
	if( m_lpBasePath )
		delete [] m_lpBasePath;

	if( m_lpRootName )
		delete [] m_lpRootName;
}
